﻿using FreeSql;
using GameServer.Database;
using GameServer.Model;
using Summer;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameServer.Mgr
{
    /// <summary>
    /// 统一管理全部的角色（创建，移除，获取）
    /// </summary>
    public class CharacterManager : Singleton<CharacterManager>
    {
        //游戏里全部的角色 <ChrId,ChrObj>
        private ConcurrentDictionary<int, Character> Characters = new ConcurrentDictionary<int, Character>();
        
        IBaseRepository<DbCharacter> repo = Db.fsql.GetRepository<DbCharacter>();
        public CharacterManager() {
            //每隔2秒保存Data到数据库
            Schedule.Instance.AddTask(Save , 2);
        }

        public Character CreateCharacter(DbCharacter dbchr)
        {
            Character chr = dbchr;
            Characters[chr.Id] = chr;
            EntityManager.Instance.AddEntity(dbchr.SpaceId, chr);
            return chr;
        }

        public void RemoveCharacter(int chrId)
        {
            
            Character chr;
            if(Characters.TryRemove(chrId, out chr))
            {
                EntityManager.Instance.RemoveEntity(chr.Data.SpaceId, chr);
            }
        }

        public Character GetCharacter(int chrId)
        {
            return Characters.GetValueOrDefault(chrId, null);
        }

        public void Clear()
        {
            Characters.Clear();
        }


        private void Save()
        {
            foreach (var chr in Characters.Values)
            {
                repo.UpdateAsync(chr.Data);
            }
        }
    }
}
